package cn.myzqu.sign.project.server.api;


import cn.myzqu.sign.common.response.ResultData;
import cn.myzqu.sign.project.client.ProjectClient;
import cn.myzqu.sign.project.common.vo.ProjectInfoVo;
import cn.myzqu.sign.project.server.entity.ProjectInfo;
import cn.myzqu.sign.project.common.input.ProjectStaffQueryInput;
import cn.myzqu.sign.project.server.service.ProjectService;
import cn.myzqu.sign.project.server.utils.TokenUtil;
import cn.myzqu.sign.project.common.input.ProjectQueryInput;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: 的川
 * @Date: 2019/3/5 16:36
 */
@RestController
public class ProjectApi implements ProjectClient {

    @Autowired
    private ProjectService projectService;

    @ApiOperation("发布一个项目")
    @Override
    public ResultData addProject(@RequestBody ProjectQueryInput projectQueryInput) throws Exception{
        String loginId = TokenUtil.getUserId();
        projectQueryInput.setCreateUserId(loginId);
        //复制值到ProjectInfo
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(projectQueryInput,projectInfo);
        Boolean result = projectService.add(projectInfo);
        if(result){
            return ResultData.success(projectQueryInput);
        }
        return ResultData.error("发布失败！");
    }

    @Override
    @ApiOperation("根据id获取项目信息")
    public ResultData getProjectInfoById(@PathVariable Long id){
        ProjectInfo projectInfo = projectService.getProjectInfoById(id);
        if(projectInfo!=null){
            return ResultData.success(projectInfo);
        }
        return ResultData.error("获取信息失败！");
    }

    @Override
    @ApiOperation("签到运营员获取管理的的项目")
    public ResultData getProjectList(@RequestBody Map params) throws Exception{
        String loginId = TokenUtil.getUserId();
        params.put("userId",loginId);
        List<Map> result = projectService.getProjectByStaff(params);
        if(result!=null){
            return ResultData.success(result);
        }
        return ResultData.error("无结果");
    }

    @Override
    @ApiOperation("签到参与者获取项目")
    public ResultData getMyProject(@RequestBody ProjectStaffQueryInput projectStaffQueryInput) throws Exception{
        String loginId = TokenUtil.getUserId();
        projectStaffQueryInput.setUserId(loginId);
        List<ProjectInfoVo> result = projectService.searchMyProject(projectStaffQueryInput);
        if(result!=null){
            return ResultData.success(result);
        }
        return ResultData.error("无结果");
    }

    @Override
    @ApiOperation("获取项目")
    public ResultData searchProject(@RequestBody ProjectQueryInput projectQueryInput){
        List<Map> result = projectService.searchProject(projectQueryInput);
        if(result!=null){
            return ResultData.success(result);
        }
        return ResultData.error("无结果");
    }
}
